package com.wtwd.device.constant;

import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;

import java.util.regex.Pattern;

/**
 * @author zdl
 * @date 2021/11/19 14:24
 */
public enum MobileRegularExpEnum {
    CN("中国", "\\+?86", "^1[345789]\\d{9}$"),
    TW("台湾", "\\+?886", "\\d{8}$"),
    HK("香港", "\\+?852", "^[569]\\d{3}\\-?\\d{4}$"),
    US("美国", "\\+?1", "\\d{10}$"),
    ZA("南非", "\\+?27", "0?\\d{9}$"),
    FR("法国", "\\+?33", "^[67]\\d{8}$"),
    GB("英国", "\\+?44", "^7\\d{9}$"),
    GH("加纳", "\\+?233", "^[^3]\\d{8}$");
    /**
     * 国际名称
     */
    private String national;
    private String areaCode;

    /**
     * 正则表达式
     */
    private String regularExp;


    public String getNational() {
        return national;
    }

    public void setNational(String national) {
        this.national = national;
    }

    public String getRegularExp() {
        return regularExp;
    }

    public void setRegularExp(String regularExp) {
        this.regularExp = regularExp;
    }

    MobileRegularExpEnum(String national, String areaCode, String regularExp) {
        this.national = national;
        this.areaCode = areaCode;
        this.regularExp = regularExp;
    }

    public static boolean checkMobileAreaCode(String mobile) {
        for (MobileRegularExpEnum expEnum : MobileRegularExpEnum.values()) {
            if (Pattern.matches(expEnum.regularExp, mobile)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isChina(String areaCode) {
        if (Pattern.matches(MobileRegularExpEnum.CN.areaCode, areaCode)) {
            return true;
        }
        return false;

    }


    public static boolean checkMobileAreaCode(String areaCode, String phone) {
        for (MobileRegularExpEnum expEnum : MobileRegularExpEnum.values()) {
            if (Pattern.matches(expEnum.areaCode, areaCode)) {
                if (Pattern.matches(expEnum.regularExp, phone)) {
                    return true;
                }
                return false;
            }

        }
        return true;
    }

    public static boolean checkAllMobileAreaCode(String areaCode, String phone) {
        try {
            Phonenumber.PhoneNumber phoneNumber = new Phonenumber.PhoneNumber().setCountryCode(Integer.parseInt(areaCode))
                    .setNationalNumber(Long.valueOf(phone));
            return instance.isValidNumber(phoneNumber);
        } catch (Exception exception) {
            return false;
        }

    }

    final static PhoneNumberUtil instance = PhoneNumberUtil.getInstance();

    public static void main(String[] args) {


    }
}
